//
//  FSRequestAction.m
//  FinancialStore
//
//  Created by zhenqiu on 16/7/19.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "FSRequestAction.h"
#import "GetDeviceMessageTool.h"
#import "CacheHelper.h"
#import "CityInfo.h"
#import "EncryptAction.h"


@implementation FSRequestAction

-(instancetype)init{
    
    if(self = [super init]){
        
        // to do
    }
    
    return self;
    
}



+(id)genFSBaseParameter{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:5];
    
    NSString * auth_did = [self getAuthDid];
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSString * auth_tms = [NSString stringWithFormat:@"%lu",(unsigned long)time];
    NSString * auth_dsig = [self genAuthDsig:auth_tms];
    NSString *app_v = [GetDeviceMessageTool AppVersionNum];
    
    id uid = [[CacheHelper getInstance] getUserId];
    NSInteger auth_uid = uid? [uid integerValue]:0;
    
    [dic setValue:auth_did forKey:@"auth_did"];
    [dic setValue:auth_tms forKey:@"auth_tms"];
    [dic setValue:auth_dsig forKey:@"auth_dsig"];
    [dic setValue:SERVER_VERSION forKey:@"v"];
    [dic setValue:app_v forKey:@"app_v"];
    [dic setValue:@(auth_uid) forKey:@"auth_uid"];
    [dic setValue:APP_NO forKey:@"app_no"];
    [dic setValue:@"AppStore" forKey:@"channel"];
//    [dic setValue:@(0) forKey:@"auth_debug"];
    
    return dic;
}



+(id) createUrl:(NSString *) urlStr{

    NSString * url = [NSString stringWithFormat:@"%@",urlStr];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];

    [dic setValue:@"open.haodai" forKey:@"source"];
    [dic setValue:@"hd_11016258" forKey:@"ref"];
    [dic setValue:@"app" forKey:@"auth"];
    if([CityInfo getCacheCity]){
        
        NSString * s_EN = [[CityInfo getCacheCity] s_EN];
        if(s_EN){
            
            [dic setValue:s_EN forKey:@"city"];
        }
        
    }
    url = [url stringByAppendingString:[self createGetParameter:dic]];

    return url;
}

+(id)createGetParameter:(id)dic{

    NSString * str = [[NSString alloc] init];

    if([dic allKeys] > 0)
    {
        str =  [str stringByAppendingString:@"?"];

    }

    for (id key in [dic allKeys]) {

        str = [str stringByAppendingString:[NSString stringWithFormat:@"%@=%@&",key,[dic objectForKey:key]]];

    }

    if([str length] > 0)
    {

        str = [str substringToIndex:[str length]-1];

    }

    return  str;
}


@end
